package com.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileChannelDemo01 {

    public static void main(String[] args) throws IOException {
        FileInputStream fileInputStream = new FileInputStream("D:\\dev\\1\\fw.txt");
        FileChannel channel = fileInputStream.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        int len=channel.read(buffer);

        while(len!=-1) {
            buffer.flip();
//            while(buffer.hasRemaining()) {
//                System.out.println((char)buffer.get());
//            }
            byte[] array = buffer.array();
            String s = new String(array,0,len);
            System.out.println(s);
            //清空缓冲区
            buffer.clear();
            len = channel.read(buffer);
        }
        channel.close();
        fileInputStream.close();
    }
}
